#!/usr/bin/env python
# coding=utf-8
"""
# @Created on : 2016/05/05 20:02
# @Author     : ldf (ldf@eehello.com)
# @Link       : http://www.eehello.com
# @Version    : 
# 本程序在python2.7下测试通过
"""

import paramiko
import threading

lock = threading.Lock()

class my_ssh(object):

    def __init__(self,host,port,user,passwd):

        self.host = host
        self.port = port
        self.user = user
        self.pw = passwd


    def ssh_cmd(self,cmd):

        self.cmd = cmd
        #lock = lock

        ssh = paramiko.SSHClient()
        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        ssh.connect(self.host,self.port,self.user,self.pw)

        stdin, stdout, stderr = ssh.exec_command(self.cmd)

        lock.acquire()
        print(u'主机%s执行了命令%s，返回结果为：%s' % (self.host,self.cmd,stdout.read()))
        lock.release()

        ssh.close()

    def ssh_up(self,loc_path,rem_path):

        self.loc_path = loc_path
        self.rem_path = rem_path

        t = paramiko.Transport((self.host,self.port))
        t.connect(username=self.user,password=self.pw)
        sftp = paramiko.SFTPClient.from_transport(t)
        sftp.put(self.loc_path,self.rem_path)

        t.close()

    def ssh_down(self,rem_path,loc_path):

        self.rem_path = rem_path
        self.loc_path = loc_path

        t = paramiko.Transport((self.host,self.port))
        t.connect(username=self.user,password=self.pw)
        sftp = paramiko.SFTPClient.from_transport(t)
        sftp.get(self.loc_path,self.rem_path)

        t.close()
